<?php
namespace Home\Controller;
use \Think\Controller;
//新闻内容控制
class SearchController extends HomeController
{

	public function index(){


		$this->assign('title','搜索页');
		$this->Search();
	}
	//搜索
	public function search(){
		// $_GET['P']=2;
		$_GET['p'] = empty($_GET['p'])?'1':$_GET['p'];
		// $key = empty($_POST['title'])?'':$_GET['title'];
		 if ($_POST['title']) {
	            $key = $_POST['title'];
	        } elseif ($_GET['title']) {
	            $key = $_GET['title'];
	        }
		$where['title'] = array('like', '%'.$key.'%');
		 // dump($where);
              // 进行分页数据查询 注意page方法的参数的前面部分是当前的页数使用 $_GET[p]获取
	        $Article = M('Article'); // 实例化Article对象
	        $list = $Article->field('u.name, a.id, a.title, a.create_time, a.content')
	                ->table('hx_article a, hx_user u')
	                ->order('id desc')
	                ->page($_GET['p'].',3')
	                ->where('a.status=1 AND a.user_id=u.id')
	                ->where($where)->select();
	        // v($list);
	        // exit;
	        $this->assign('data',$list);// 赋值数据集
	        $count = $Article->table('hx_article a, hx_user u')
	                ->where('a.status=1 AND a.user_id=u.id')
	                ->where($where)->count();// 查询满足要求的总记录数
	        // dump($count);
	        $Page = new \Think\Page($count,3);// 实例化分页类 传入总记录数和每页显示的记录数
	        $Page->parameter["title"] = $title;
	        $show = $Page->show();// 分页显示输出
	        // echo'<pre>';
	        // dump($show);
	        $this->assign('page',$show);// 赋值分页输出
		  //r热文
		 $hot = M('category')
	              ->field('a.title, i.img, a.id aid')
	              ->table('hx_article a, hx_image i, hx_user u')
	              ->where('u.id=a.user_id AND i.art_id=a.id AND a.status=1')
	              ->order('a.read_times desc')
	              ->limit(3)
	              ->select();
	             // v($hot);
	        $this->assign('hot', $hot);
	        $this->display('Search/index');

	}



}